using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HttpModels;

/// <summary>
/// HTTP消息收发示例
/// </summary>
public class HttpExample : MonoBehaviour
{
    void Start()
    {
        // 确保HttpNetworkManager存在
        if (HttpNetworkManager.Instance == null)
        {
            gameObject.AddComponent<HttpNetworkManager>();
        }

        // 创建登录请求
        var loginRequest = new LoginRequest("player1", "123456", "device_001", "Android");
        string jsonRequest = JsonUtil.ToJson(loginRequest);

        // 发送登录请求
        HttpNetworkManager.Instance.SendPostRequest("/api/login", jsonRequest, OnLoginResponse);
    }

    /// <summary>
    /// 登录响应处理
    /// </summary>
    /// <param name="success">是否成功</param>
    /// <param name="response">响应数据</param>
    private void OnLoginResponse(bool success, string response)
    {
        if (success)
        {
            Debug.Log("收到登录响应: " + response);

            // 解析响应数据
            var loginResponse = JsonUtil.FromJson<LoginResponse>(response, new LoginResponse());

            if (loginResponse.code == 0)
            {
                Debug.Log("登录成功!");
                Debug.Log($"欢迎, {loginResponse.playerInfo.nickname} (等级: {loginResponse.playerInfo.level})");
                Debug.Log($"金币: {loginResponse.playerInfo.gold}, 钻石: {loginResponse.playerInfo.diamond}");
                Debug.Log($"登录令牌: {loginResponse.token}");

                // 显示道具信息
                if (loginResponse.playerInfo.items.Count > 0)
                {
                    Debug.Log("道具列表:");
                    foreach (var item in loginResponse.playerInfo.items)
                    {
                        Debug.Log($"  - 道具ID: {item.itemId}, 数量: {item.count}");
                    }
                }

                // 显示英雄信息
                if (loginResponse.playerInfo.heroes.Count > 0)
                {
                    Debug.Log("英雄列表:");
                    foreach (var hero in loginResponse.playerInfo.heroes)
                    {
                        Debug.Log($"  - 英雄ID: {hero.heroId}, 等级: {hero.level}, 星级: {hero.star}");
                    }
                }
            }
            else
            {
                Debug.LogError($"登录失败: {loginResponse.message} (错误码: {loginResponse.code})");
            }
        }
        else
        {
            Debug.LogError("网络请求失败: " + response);
        }
    }

    void Update()
    {
        // 按空格键发送另一个登录请求
        if (Input.GetKeyDown(KeyCode.Space))
        {
            var loginRequest = new LoginRequest("player2", "123456", "device_002", "iOS");
            string jsonRequest = JsonUtil.ToJson(loginRequest);
            HttpNetworkManager.Instance.SendPostRequest("/api/login", jsonRequest, OnLoginResponse);
        }

        // 按回车键测试错误登录
        if (Input.GetKeyDown(KeyCode.Return))
        {
            var loginRequest = new LoginRequest("unknown", "wrong", "device_003", "PC");
            string jsonRequest = JsonUtil.ToJson(loginRequest);
            HttpNetworkManager.Instance.SendPostRequest("/api/login", jsonRequest, OnLoginResponse);
        }
    }
}